package com.research.controller;

import java.util.HashMap;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.research.domain.entity.EducationStat;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.common.annotation.Log;
import com.common.core.controller.BaseController;
import com.common.core.domain.AjaxResult;
import com.common.enums.BusinessType;
import com.research.domain.entity.ResearchPerson;
import com.research.service.IResearchPersonService;
import com.common.utils.poi.ExcelUtil;
import com.common.core.page.TableDataInfo;

/**
 * 研究机构-机构人员列Controller
 *
 * @author hst
 * @date 2025-03-24
 */
@RestController
@RequestMapping("/research/person")
public class ResearchPersonController extends BaseController
{
    @Autowired
    private IResearchPersonService researchPersonService;

    /**
     * 查询研究机构-机构人员列列表
     */
    @PreAuthorize("@ss.hasPermi('research:person:list')")
    @GetMapping("/list")
    public TableDataInfo list(ResearchPerson researchPerson)
    {
        startPage();
        List<ResearchPerson> list = researchPersonService.selectResearchPersonList(researchPerson);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('research:person:list')")
    @GetMapping("/getDutyStatusCount")
    public AjaxResult selectDutyStatusCount(ResearchPerson researchPerson)
    {
        researchPerson.setDutyStatus(null);
        HashMap<String, Integer> map = researchPersonService.selectDutyStatusCount(researchPerson);
        return success(map);
    }

    /**
     * 导出研究机构-机构人员列列表
     */
    @PreAuthorize("@ss.hasPermi('research:person:export')")
    @Log(title = "研究机构-机构人员列", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ResearchPerson researchPerson)
    {
        List<ResearchPerson> list = researchPersonService.selectResearchPersonList(researchPerson);
        ExcelUtil<ResearchPerson> util = new ExcelUtil<ResearchPerson>(ResearchPerson.class);
        util.exportExcel(response, list, "研究机构-机构人员列数据");
    }

    /**
     * 获取研究机构-机构人员列详细信息
     */
    @PreAuthorize("@ss.hasPermi('research:person:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(researchPersonService.selectResearchPersonById(id));
    }

    /**
     * 新增研究机构-机构人员列
     */
    @PreAuthorize("@ss.hasPermi('research:person:add')")
    @Log(title = "研究机构-机构人员列", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ResearchPerson researchPerson)
    {
        if(researchPerson.getTypeId().equals("in")) {
            ResearchPerson temp = new ResearchPerson();
            temp.setPersonId(researchPerson.getPersonId());
            temp.setResearchUnitId(researchPerson.getResearchUnitId());
            List<ResearchPerson> list = researchPersonService.selectResearchPersonList(temp);
            if (list.size() > 0) {
                return AjaxResult.error("同一个机构不能重复添加同一个人员！");
            }
        }
        return toAjax(researchPersonService.insertResearchPerson(researchPerson));
    }

    /**
     * 修改研究机构-机构人员列
     */
    @PreAuthorize("@ss.hasPermi('research:person:edit')")
    @Log(title = "研究机构-机构人员列", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ResearchPerson researchPerson)
    {
        if(researchPerson.getTypeId().equals("in")){
            ResearchPerson temp = new ResearchPerson();
            temp.setPersonId(researchPerson.getPersonId());
            temp.setResearchUnitId(researchPerson.getResearchUnitId());
            List<ResearchPerson> list = researchPersonService.selectResearchPersonList(temp);
            if (list.size() > 1 || (list.size() == 1 && !list.get(0).getId().equals(researchPerson.getId()))) {
                return AjaxResult.error("同一个机构不能重复添加同一个人员！");
            }
        }
        return toAjax(researchPersonService.updateResearchPerson(researchPerson));
    }

    /**
     * 删除研究机构-机构人员列
     */
    @PreAuthorize("@ss.hasPermi('research:person:remove')")
    @Log(title = "研究机构-机构人员列", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(researchPersonService.deleteResearchPersonByIds(ids));
    }
}
